<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\CommonApi;
use App\Http\Requests\AdminBrandRequest;
use App\Model\Goodsbrand;
use Illuminate\Support\Facades\Redis;
use DB;

class AdminGoodsBrandController extends Controller
{

	/**
	 * 显示品牌页面
	 */
    public function AdminGoodsBrand()
    {
    	$res = DB::table('shop_goods_brands')->select('id', 'brand_name', 'brand_logo', 'brand_desc', 'site_url')->get();
    	// dd($res);
    	return view('Admin.goods.product-brand', ['res' => $res]);
    }

    /**
     * 显示添加品牌页
     */
    public function AdminGoodsBrandAdd()
    {
    	return view('Admin.goods.product-brandadd');
    }

    /**
     * 处理添加品牌
     */
    public function AdminGoodsAddBrand(AdminBrandRequest $request)
    {
    	
    	$files = new CommonApi();

    
    	$res = $files->uploadToQiNiu($request, 'picurl');
    
    	//判断失败返回的状态码
    	if ($res['status'] == 4403) {
    		return back()->with('msg', '没有文件上传');
    	}
    	if ($res['status'] == 4401) {
    		return back()->with('msg', '上传文件不合法');
    	}
    	if ($res['status'] == 4044) {
    		return back()->with('msg', '上传失败');
    	}

    	//接收传过来的数据
    	$name = $request->input('name');
    	$desc = $request->input('desc');
    	$url = $request->input('site_url');
    	$files = $request->file('picurl');
        $created_at = date('Y:m:d H:i:s');
        $updated_at = date('Y:m:d H:i:s');

    	if (!$files) {

    		return back()->with('msg', '不是一个文件');
    	}

    	// dd($res['data']['key']);exit;
    	$img = $res['data']['key'];

    	$id = DB::table('shop_goods_brands')->insertGetId([
    			'brand_name' => $name, 
    			'brand_logo' => $img, 
    			'brand_desc' => $desc, 
    			'site_url'   => $url,
                'created_at' => $created_at,
                'updated_at' => $updated_at
    	]);

    	if ($id == 0) {

    		return back()->with('msg', '添加失败');

    	} 
    	
        $setBrandKey = 'goods_brands_set_data';
        $hashBrandKey = 'goods_brands_hash_data:'.$id;
        //添加哈希的键到集合里
        Redis::sAdd($setBrandKey, $hashBrandKey);
        //把数据存到哈希
        Redis::hMSet($hashBrandKey, ['id' => $id, 'brand_name' => $name, 'brand_logo' => $img]);

    	return back()->with('msg', '添加成功');

    }


    /**
     * 处理删除品牌
     */
    public function AdminGoodsDelBrand(Request $request)
    {
    	$id = $request->id;

    	//根据品牌id查询一条数据
    	$res = Goodsbrand::find($id);
    	// dd($res);
    	// 根据品牌id去商品表查询数据，看是否品牌下有商品
    	$goods = $res->goods;

    	//进去表示有商品
    	if (!empty($goods)) {

    		return [
	            'msg' => '请先删除该品牌下的商品',
	            'status' => 404,
            ];
    	}

    	//删除品牌
    	$lol = Goodsbrand::where('id', $id)->delete();

        if (!$lol) {

            return [
            'msg' => '删除失败',
            'status' => 404,
            ];
        }

        //删除七牛云图片
        $files = new CommonApi();
        $files->deleteToQiNiu('shop_goods_brand', 'brand_logo', $id);
        
        $setBrandKey = 'goods_brands_set_data';
        $hashBrandKey = 'goods_brands_hash_data:'.$id;
        //删除集合中的元素
        Redis::sRem($setBrandKey, $hashBrandKey);
        //删除哈希的键
        Redis::del($hashBrandKey);

    	return [
    		'msg' => '已删除',
    		'status' => 200,
    	];
    }


    /**
     * 显示编辑品牌页
     */
    public function AdminGoodsEditBrand(Request $request)
    {
    	$id = $request->id;

    	$res = Goodsbrand::find($id);
    	// dd($res->brand_name);
    	return view('Admin.goods.product-brandedit', ['res' => $res]);
    }


    /**
     * 处理编辑品牌
     */
    public function AdminGoodsBrandEdit(AdminBrandRequest $req)
    {
    	$id = $req->input('id');
    	$data['brand_name'] = $req->input('name');
    	$data['site_url'] = $req->input('site_url');
    	$data['brand_desc'] = $req->input('desc');
        $data['updated_at'] = date('Y:m:d H:i:s');
    	

    	//判断是否修改图片
    	if (!empty($req->file('picurl'))) {

    		$files = new CommonApi();

	    
	    	$res = $files->uploadToQiNiu($req, 'picurl');
	    
	    	//判断失败返回的状态码
	    	if ($res['status'] == 4403) {
	    		return back()->with('msg', '没有文件上传');
	    	}
	    	if ($res['status'] == 4401) {
	    		return back()->with('msg', '上传文件不合法');
	    	}
	    	if ($res['status'] == 4044) {
	    		return back()->with('msg', '上传失败');
	    	}

	    	$data['brand_logo'] = $res['data']['key'];

	    	$files->deleteToQiNiu('shop_goods_brands', 'brand_logo', $id);
    	}

    	//修改数据库
    	$lol = Goodsbrand::where('id', $id)->update($data);
        if (!$lol) {

            return back()->with('msg', '修改失败');;

        }


        $setBrandKey = 'goods_brands_set_data';
        $hashBrandKey = 'goods_brands_hash_data:'.$id;
        //删除集合的元素
        Redis::sRem($setBrandKey, $hashBrandKey);
        //删除哈希
        Redis::del($hashBrandKey);

        //把数据查出来存入Redis
        $brand = Goodsbrand::find($id);
        //把哈希键添加到集合中
        Redis::sAdd($setBrandKey, $hashBrandKey);
        //把数据存到哈希
        Redis::hMSet($hashBrandKey, ['id' => $brand->id, 'brand_name' => $brand->brand_name, 'brand_logo' => $brand->brand_logo]);

    	return back()->with('msg', '修改成功');
    }
}
